<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="角色" prop="role">
        <el-input
          v-model="queryParams.role"
          placeholder="请输入角色"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="加入时间" prop="joinTime">
        <el-date-picker clearable
          v-model="queryParams.joinTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择加入任务时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['erp:taskMember:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['erp:taskMember:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['erp:taskMember:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
      v-loading="loading" 
      :data="taskMemberList" 
      @selection-change="handleSelectionChange"
      class="task-member-table"
      stripe
      border
      :header-cell-style="{ background: '#f5f7fa', color: '#606266', fontWeight: '600' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="任务ID" align="center" prop="taskId" width="120" show-overflow-tooltip>
        <template #default="scope">
          <div class="task-info">
            <el-icon class="task-icon"><Document /></el-icon>
            <span>{{ scope.row.taskId || '暂无' }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="任务名称" align="center" prop="taskName" width="180" show-overflow-tooltip>
        <template #default="scope">
          <div class="task-name-info" v-if="scope.row.taskName">
            <el-icon class="task-name-icon"><Folder /></el-icon>
            <span class="task-name-text">{{ scope.row.taskName }}</span>
          </div>
          <span v-else class="text-muted">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="成员" align="center" prop="userId" width="150">
        <template #default="scope">
          <div class="user-info" v-if="scope.row.userId">
            <el-avatar :size="24" class="user-avatar">
              <el-icon><User /></el-icon>
            </el-avatar>
            <span class="user-name">{{ scope.row.userName || scope.row.userId }}</span>
          </div>
          <span v-else class="text-muted">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="加入时间" align="center" prop="joinTime" width="120">
        <template #default="scope">
          <div class="date-info" v-if="scope.row.joinTime">
            <el-icon class="date-icon"><Calendar /></el-icon>
            <span>{{ parseTime(scope.row.joinTime, '{y}-{m}-{d}') }}</span>
          </div>
          <span v-else class="text-muted">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark" min-width="200" show-overflow-tooltip>
        <template #default="scope">
          <div class="remark-info" v-if="scope.row.remark">
            <el-icon class="remark-icon"><Document /></el-icon>
            <span class="remark-text">{{ scope.row.remark }}</span>
          </div>
          <span v-else class="text-muted">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template #default="scope">
          <el-button 
            link 
            type="danger" 
            icon="Delete" 
            @click="handleDelete(scope.row)" 
            v-hasPermi="['erp:taskMember:remove']"
            class="action-btn"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务成员对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taskMemberRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务ID" />
        </el-form-item>
        <el-form-item label="成员ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入成员ID" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="加入任务时间" prop="joinTime">
          <el-date-picker clearable
            v-model="form.joinTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择加入任务时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TaskMember">
import { listTaskMember, delTaskMember, addTaskMember } from "@/api/erp/taskMember"
import { Document, User, Calendar, Folder } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

const taskMemberList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: null,
    userName: null,
    role: null,
    joinTime: null,
  },
  rules: {
    taskId: [
      { required: true, message: "任务ID不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "成员ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询任务成员列表 */
function getList() {
  loading.value = true
  listTaskMember(queryParams.value).then(response => {
    taskMemberList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    taskId: null,
    userId: null,
    role: null,
    joinTime: null,
    remark: null
  }
  proxy.resetForm("taskMemberRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加任务成员"
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["taskMemberRef"].validate(valid => {
    if (valid) {
      addTaskMember(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功")
        open.value = false
        getList()
      })
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除任务成员编号为"' + _ids + '"的数据项？').then(function() {
    return delTaskMember(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('erp/taskMember/export', {
    ...queryParams.value
  }, `taskMember_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
/* 表格样式优化 */
.task-member-table {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.task-member-table :deep(.el-table__body-wrapper) {
  border-radius: 0 0 8px 8px;
}

.task-member-table :deep(.el-table__header-wrapper) {
  border-radius: 8px 8px 0 0;
}

.task-member-table :deep(.el-table__row) {
  transition: all 0.3s ease;
}

.task-member-table :deep(.el-table__row:hover) {
  background-color: #f8f9fa !important;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 任务信息样式 */
.task-info {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.task-icon {
  color: #67c23a;
  font-size: 16px;
}

/* 任务名称样式 */
.task-name-info {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.task-name-icon {
  color: #409eff;
  font-size: 16px;
}

.task-name-text {
  font-size: 13px;
  color: #303133;
  font-weight: 500;
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.user-avatar {
  background-color: #e6f7ff;
  color: #1890ff;
  border: 1px solid #b3d8ff;
}

.user-name {
  font-size: 13px;
  color: #303133;
  font-weight: 500;
}

/* 日期信息样式 */
.date-info {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;
}

.date-icon {
  color: #67c23a;
  font-size: 14px;
}

/* 备注信息样式 */
.remark-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.remark-icon {
  color: #909399;
  font-size: 14px;
  flex-shrink: 0;
}

.remark-text {
  color: #606266;
  font-size: 13px;
  line-height: 1.4;
}

/* 暂无文本样式 */
.text-muted {
  color: #c0c4cc;
  font-size: 12px;
  font-style: italic;
}

/* 操作按钮样式 */
.action-btn {
  margin: 0 2px;
  font-size: 12px;
  padding: 4px 8px;
}

.action-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 状态标签优化 */
.task-member-table :deep(.el-tag) {
  border-radius: 12px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .task-member-table :deep(.el-table__header-wrapper),
  .task-member-table :deep(.el-table__body-wrapper) {
    font-size: 12px;
  }
  
  .user-info {
    flex-direction: column;
    gap: 4px;
  }
  
  .task-info {
    flex-direction: column;
    gap: 4px;
  }
  
  .remark-info {
    flex-direction: column;
    gap: 4px;
    align-items: flex-start;
  }
}
</style>
